package com.huibei.wateradmin.authority.util;

import java.util.Random;

/**
 * Created by sunzf on 2016/6/28.
 */
public class RandomId {
    private static Random random;
    private static String table;

    private static final int ORDERID_SEED = 509;
    private static final int ORDERID_KEY = 257;

    static{
        random = new Random();
        table = "0123456789";
    }

    public static String randomId(long id){
        String ret = null;
        String num = String.format("%010d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num + String.format("%01d", key) + String.format("%02d", seed);
        return ret;
    }

    /**
     * 生成八位的订单编号，每天订单的后缀和订单验证码使用。<br/>
     * 其中6位数是根据自增key生成的，不会重复。可以保证百万内数字不会重复。<br/>
     * 真正的订单号前面带有日期前缀，每天前缀不一样，所以每天支持的最大订单数量是1百万。
     * @param id
     * @return
     */
    public static String randomOrderId(long id){
        String ret = null;

        id %= 1000000;
        String num = String.format("%06d", id);

        int rnd = random.nextInt(100);

        Caesar caesar = new Caesar(table, ORDERID_SEED);
        num = caesar.encode(ORDERID_KEY, num);
        ret = num + String.format("%02d", rnd);//增加两位随机数，凑成8位
        return ret;
    }

    /**
     * 生成返券的商品编号（“FQ” + 13位随机数）
     *
     */

    public static String randomProductNo(long id){
        String ret = null;
        String num = String.format("%010d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = "FQ" + num + String.format("%01d", key) + String.format("%02d", seed);
        return ret;
    }

    /**
     * 生成4位验证码
     *
     * @param id
     * @return
     */
    public static String randomVerifyCode(long id){
        String ret = null;

        if(id >= 1000){
            id %=1000;
        }

        String num = String.format("%03d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num + String.format("%01d", key);
        return ret;
    }

    /**
     * 生成支付码，6位
     * @return
     */
    public static String randomPayCode(long id){
        String ret = null;

        if(id >= 1000){
            id %=1000;
        }

        String num = String.format("%03d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num + String.format("%01d", key) + String.format("%02d", seed);
        return ret;
    }

    /***
     * 生成领取业务时的唯一验证码
     * @param id
     * @return
     */
    public static String randomUniqueCode(Long id) {
        String ret = null;
        String num = String.format("%09d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num + String.format("%01d", key) + String.format("%02d", seed);
        return ret;
    }

    public static String serialNumber(){
        return null;
    }

	// public static void main(String[] args){
//		for(long i = 0; i < 30; i += 1){
//			System.out.println(RandomId.randomUniqueCode(i));
//		}

	// HashSet<String> codeSet = new HashSet<String>();
	// for(long i = 1000 ; i < 2380; ++ i){
	// String orderId = randomOrderId(i);
	// String oid = orderId;
	// if(codeSet.contains(oid)){
	// System.err.println("duplicat id:" + oid);
	// }
	// codeSet.add(oid);
	//
	// if(i % 200 == 0){
	// System.out.println("code:" + oid);
	// }
	// }
	// }

    /**
     * 生成储值卡卡号
     */
    public static String randomValueCardCode(long id){
        String ret = null;
        String num = String.format("%07d", id);

        int key = random.nextInt(10);
        int seed = random.nextInt(100);

        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);
        ret = num + String.format("%01d", key) + String.format("%02d", seed);
        return ret;
    }

    /**
     * 生成指定长度的字符串id
     * @param id
     * @param len
     * @return
     */
    public static String randomFixedLengthId(long id, int len, boolean zeroStart){
        String ret = null;
        if(len <= 0){
            ret = "";
        }else if(len <= 2){
            ret = "" + id % Math.round(Math.pow(10, len));
        }else {
            id %= Math.round(Math.pow(10, len - 2));

            String num = String.format("%0" + (len -2) +"d", id);

            int key = random.nextInt(10);
            if(!zeroStart){
                while(key == 0){
                    key = random.nextInt(10);
                }
            }
            int seed = random.nextInt(10);
            Caesar caesar = new Caesar(table, seed);
            num = caesar.encode(key, num);
            ret = key + num + seed;

        }
        return ret;
    }
}
